import java.util.HashSet;

class Solution {
    /**
     * 136. 只出现一次的数字
     * https://leetcode.cn/problems/single-number/description/
     * @param nums
     * @return
     */
    public int singleNumber1(int[] nums) {
        int ret = nums[0];
        for(int i = 1; i < nums.length; i++) {
            ret ^= nums[i];
        }
        return ret;
    }

    /**
     * 260. 只出现一次的数字 III
     * https://leetcode.cn/problems/single-number-iii/description/
     */
    //使用hash容器
    public int[] singleNumber2(int[] nums) {
        HashSet<Integer> set = new HashSet<>();
        for(int x : nums) {
            if(!set.contains(x)) {
                set.add(x);
            } else {
                set.remove(x);
            }
        }
        int[] ret = new int[2];
        int count = 0;
        for(int x : nums) {
            if(set.contains(x)) {
                ret[count++] = x;
            }
        }
        return ret;
    }

}